/*
 * @Title: community
 * @Description: TODO
 * @author MI
 * @date 2022/7/29 14:42
 * @version
 */
package com.shanshan.community.controller;

import com.shanshan.community.entity.DiscussPost;
import com.shanshan.community.entity.User;
import com.shanshan.community.service.DiscussPostService;
import com.shanshan.community.util.CommunityUtil;
import com.shanshan.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author MI
 * @ClassName: DiscussPostController
 * @Description: 帖子的控制器
 * @date 2022/7/29 14:42
 */

@Controller
@RequestMapping("/discuss")
public class DiscussPostController {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private HostHolder hostHolder;


    @PostMapping("/add")
    @ResponseBody
    public String addDiscussPost(String title, String content) {
        User user = hostHolder.getUser();
        // 先判断用户是否登录
        if (user == null) {
            return CommunityUtil.getJSONString(403, "你要先登录呀 宝");
        }

        DiscussPost discussPost = new DiscussPost();
        discussPost.setContent(content);
        discussPost.setTitle(title);
        discussPost.setCreateTime(new Date());
        discussPost.setUserId(user.getId());
        discussPostService.addDisCussPost(discussPost);

        // 报错在之后统一进行处理
        return CommunityUtil.getJSONString(0, "发布成功啦");
    }
}
